import { Component, OnInit } from '@angular/core';
import { ToastController } from '@ionic/angular';
import { ApiUrl } from '../api/ApiUrl';
import { ApiService } from '../api/api.service';
import { AnswerVO } from '../vo/Answervo';
import { Router } from '@angular/router';
import { UserVO } from '../vo/UserVO';
@Component({
  selector: 'app-update-pwd',
  templateUrl: './update-pwd.page.html',
  styleUrls: ['./update-pwd.page.scss'],
})
export class UpdatePwdPage implements OnInit {
  newpwd: string;
  oldpwd: string;
  confirmpwd: string;
  item: AnswerVO;
  userItem: UserVO;
  constructor(private toastController: ToastController, public api: ApiService, private router: Router) { }

  ngOnInit() {
    this.userItem = ApiUrl.user;
  }
submit() {
  if (undefined === this.newpwd || this.newpwd.length === 0) {
    this.presentToast('请输入原密码');
    return;
  }
  if (undefined === this.oldpwd || this.oldpwd.length === 0) {
    this.presentToast('请输入新密码');
    return;
  }
  if (undefined === this.confirmpwd || this.confirmpwd.length === 0) {
    this.presentToast('请确认新密码');
    return;
  }
  if ( this.confirmpwd !== this.newpwd) {
    this.presentToast('两次密码输入不一致！');
    return;
  }
  this.api.getItem<AnswerVO>(ApiUrl.updatepwd, { 'oldpwd': this.oldpwd,
  'newpwd': this.newpwd, 'iid': this.userItem.iid}).subscribe(response => {
    this.item = response;
    if (this.item.success) {
      this.presentToast(this.item.msg);
      ApiUrl.user = null;
      this.router.navigate(['/login']);
    } else {
      this.presentToast(this.item.msg);
    }
  }, () => {
    console.log('出错了');
  });
}

async presentToast(msg) {
  const toast = await this.toastController.create({
    message: msg,
    duration: 2000
  });
  toast.present();
}
}
